﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TemplateWeb.Helpers;
using TemplateWeb.Models;

namespace TemplateWeb.Controllers
{
    [HandleError]
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.HOME_INDEX
            };
            return View(CreateViewModel(data));
        }

        public ActionResult GioiThieu()
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.HOME_GIOITHIEU
            };
            return View(CreateViewModel(data));
        }

        public ActionResult SelectTinTuc()
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.HOME_TINTUC
            };
            return View("TinTuc", CreateViewModel(data));
        }

        public ActionResult SelectTinTucByLoaiTinTuc(int idloaitintuc, int page = 1)
        {
            var data = new DataTransferViewModel
            {
                IDLoaiTinTuc = idloaitintuc,
                EnumViewModelType = EnumViewModel.HOME_TINTUC,
                CurrentPage = page
            };
            return View("TinTuc", CreateViewModel(data));
        }

        public ActionResult ChiTietTinTuc(int id)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.HOME_DETAILOF_TINTUC,
                EditID = id
            };
            return View(CreateViewModel(data));
        }

        public ActionResult ChiTietCamNang(int id)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.HOME_DETAILOF_CAMNANG,
                EditID = id
            };
            return View(CreateViewModel(data));
        }

        public ActionResult SelectCamNang()
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.HOME_CAMNANG
            };
            return View("CamNang", CreateViewModel(data));
        }

        public ActionResult SelectCamNangByLoaiCamNang(int idloaicamnang, int page = 1)
        {
            var data = new DataTransferViewModel
            {
                IDLoaiCamNang = idloaicamnang,
                EnumViewModelType = EnumViewModel.HOME_CAMNANG,
                CurrentPage = page
            };
            return View("CamNang", CreateViewModel(data));
        }

        public ActionResult SelectDatNen()
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.HOME_NHADAT_DATNEN
            };
            ViewData["NhaDatHeader"] = "Danh sách đất nền";
            return View("NhaDat", CreateViewModel(data));
        }

        public ActionResult SelectCanHo()
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.HOME_NHADAT_CANHO
            };
            ViewData["NhaDatHeader"] = "Danh sách căn hộ";
            return View("NhaDat", CreateViewModel(data));
        }

        public ActionResult SelectNhaDatByLoaiBatDongSan(int idloaibatdongsan, int page = 1)
        {
            var data = new DataTransferViewModel
            {
                IDLoaiBatDongSan = idloaibatdongsan,
                EnumViewModelType = EnumViewModel.HOME_NHADAT,
                CurrentPage = page
            };
            string tenloaibds = _repLoaiBatDongSan.SelectByID(idloaibatdongsan).TenLoaiBatDongSan.ToUpper();
            if (tenloaibds.Contains(WebConfiguration.DatNen))
            {
                if (tenloaibds.Contains(WebConfiguration.NhaDatDuAn))
                    ViewData["NhaDatHeader"] = "Danh sách đất nền dự án";
                else
                    ViewData["NhaDatHeader"] = "Danh sách đất nền lẻ";
            }
            else
            {
                if (tenloaibds.Contains(WebConfiguration.NhaDatDuAn))
                    ViewData["NhaDatHeader"] = "Danh sách căn hộ dự án";
                else
                    ViewData["NhaDatHeader"] = "Danh sách căn hộ lẻ";
            }
            return View("NhaDat", CreateViewModel(data));
        }

        public ActionResult ChiTietNhaDatDuAn(int id)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.HOME_DETAILOF_NHADAT,
                EditID = id
            };
            return View("ChiTietNhaDatDuAn", CreateViewModel(data));
        }

        public ActionResult ChiTietNhaDatLe(int id)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.HOME_DETAILOF_NHADAT,
                EditID = id
            };
            string tenloaibds = _repNhaDat.SelectByID(id).LoaiBatDongSan.TenLoaiBatDongSan.ToUpper();
            if (tenloaibds.Contains(WebConfiguration.CanHo))
                return View("ChiTietCanHoLe", CreateViewModel(data));
            return View("ChiTietNhaDatLe", CreateViewModel(data));
        }

        public ActionResult LienHe()
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.HOME_HOPTHU
            };
            return View(CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertHopThu(FormCollection forms)
        {
            string hoten = forms.GetValues("HopThu_HoTen")[0];
            string email = forms.GetValues("HopThu_Email")[0];
            string tieude = forms.GetValues("HopThu_TieuDe")[0];
            string noidung = forms.GetValues("HopThu_NoiDung")[0];

            var newItem = new HopThu
            {
                HoTen = hoten,
                Email = email,
                TieuDe = tieude,
                NoiDung = noidung,
                NgayDang = DateTime.Now
            };
            var result = _repHopThu.Insert(newItem);
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.HOME_HOPTHU,
                Added = result
            };
            return View("LienHe", CreateViewModel(data));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DangTin(FormCollection forms)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.HOME_DANGTIN,
                IDTinhThanh = _repTinhThanh.SelectAll()[0].ObjModel.ID,
                UserName = forms.GetValues("NguoiDung_UserName")[0]
            };
            return View(CreateViewModel(data));
        }

        public ActionResult DangTin(int idtinhthanh, string username)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.HOME_DANGTIN,
                IDTinhThanh = idtinhthanh,
                UserName = username
            };
            return View(CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertNhaDatLe(FormCollection forms)
        {
            string idloaibatdongsan = forms.GetValues("NhaDat_IDLoaiBatDongSan")[0];
            string idquanhuyen = forms.GetValues("NhaDat_IDQuanHuyen")[0];
            string idloaigia = forms.GetValues("NhaDat_IDLoaiGia")[0];
            string idnguoidang = forms.GetValues("NhaDat_IDNguoiDang")[0];
            string username = forms.GetValues("NhaDat_UserName")[0];
            string gia = forms.GetValues("NhaDat_Gia")[0];
            string diachi = forms.GetValues("NhaDat_DiaChi")[0];
            string dientichngang = forms.GetValues("NhaDat_DienTichNgang")[0];
            string dientichdoc = forms.GetValues("NhaDat_DienTichDoc")[0];
            string dientichxaydung = forms.GetValues("NhaDat_DienTichXayDung")[0];
            string dientichsudung = forms.GetValues("NhaDat_DienTichSuDung")[0];
            string cosohong = forms.GetValues("NhaDat_CoSoHong")[0];
            string tieude = forms.GetValues("NhaDat_TieuDe")[0];
            string noidung = forms.GetValues("NhaDat_NoiDung")[0];
            string noidungrutgon = forms.GetValues("NhaDat_NoiDungRutGon")[0];
            string ngaydang = forms.GetValues("NhaDat_NgayDang")[0];
            string tinvip = forms.GetValues("NhaDat_TinVip")[0];
            string tieubieu = forms.GetValues("NhaDat_TieuBieu")[0];
            var file = Request.Files["NhaDat_HinhMinhHoa"];
            string refName = "";
            var newItem = new NhaDat
            {
                IDLoaiBatDongSan = int.Parse(idloaibatdongsan),
                IDQuanHuyen = int.Parse(idquanhuyen),
                IDLoaiGia = int.Parse(idloaigia),
                IDNguoiDang = Guid.Parse(idnguoidang),
                Gia = gia == "" ? 0 : long.Parse(gia),
                DiaChi = diachi,
                DienTichNgang = dientichngang == "" ? 0 : int.Parse(dientichngang),
                DienTichDoc = dientichdoc == "" ? 0 : int.Parse(dientichdoc),
                DienTichXayDung = dientichxaydung == "" ? 0 : double.Parse(dientichxaydung),
                DienTichSuDung = dientichsudung == "" ? 0 : double.Parse(dientichsudung),
                CoSoHong = bool.Parse(cosohong),
                TieuDe = tieude,
                NoiDung = noidung,
                NoiDungRutGon = noidungrutgon,
                NgayDang = DateTime.Parse(ngaydang),
                TinVip = bool.Parse(tinvip),
                TieuBieu = bool.Parse(tieubieu),
                HinhMinhHoa = "nonhadat.jpg",
                SoLanXem = 0
            };
            var result = _repNhaDat.Insert(newItem);
            if (result)
            {
                if (file.ContentLength != 0)
                {
                    result = UploadFile(file, "/Content/Images/Items/", newItem.ID.ToString(), ref refName);
                    if (result)
                    {
                        var addedItem = _repNhaDat.SelectByID(newItem.ID);
                        addedItem.HinhMinhHoa = refName;
                        _repNhaDat.Save();
                    }
                }
            }
            if (result)
            {
                InsertVanHoaXaHoi(forms, newItem.ID);
                if (newItem.LoaiBatDongSan.TenLoaiBatDongSan.ToUpper().Contains(WebConfiguration.CanHo))
                {
                    //add tien ich
                    InsertTienIch(forms, newItem.ID);
                    //add tien ich khac
                    InsertTienIchKhac(forms, newItem.ID);
                    //add thong tin can ho le
                    InsertThongTinCanHoLe(forms, newItem.ID);
                }
            }
            var data = new DataTransferViewModel
            {
                IDLoaiBatDongSan = int.Parse(idloaibatdongsan),
                IDQuanHuyen = int.Parse(idquanhuyen),
                EnumViewModelType = EnumViewModel.HOME_DANGTIN,
                Added = result,
                UserName = username
            };
            return View("DangTin", CreateViewModel(data));
        }

        public void InsertThongTinCanHoLe(FormCollection forms, int idnhadat)
        {
            string sotang = forms.GetValues("NhaDat_SoTang")[0];
            string sophongngu = forms.GetValues("NhaDat_SoPhongNgu")[0];
            string namxaydung = forms.GetValues("NhaDat_NamXayDung")[0];
            string sophongtam = forms.GetValues("NhaDat_SoPhongTam")[0];
            string huong = forms.GetValues("NhaDat_Huong")[0];
            string loainoithat = forms.GetValues("NhaDat_LoaiNoiThat")[0];
            string namsuachua = forms.GetValues("NhaDat_NamSuaChua")[0];
            string nohau = forms.GetValues("NhaDat_NoHau")[0];
            string mattien = forms.GetValues("NhaDat_MatTien")[0];
            var newItem = new ThongTinCanHoLe
            {
                IDNhaDat = idnhadat,
                SoTang = sotang,
                SoPhongNgu = sophongngu,
                NamXayDung = namxaydung,
                SoPhongTam = sophongtam,
                Huong = huong,
                LoaiNoiThat = loainoithat,
                NamSuaChua = namsuachua,
                NoHau = bool.Parse(nohau),
                MatTien = bool.Parse(mattien)
            };
            var result = _repThongTinCanHoLe.Insert(newItem);
        }

        public void InsertTienIch(FormCollection forms, int idnhadat)
        {
            string donghodien = forms.GetValues("TienIch_DongHoDien")[0];
            string donghonuoc = forms.GetValues("TienIch_DongHoNuoc")[0];
            string mayphatdien = forms.GetValues("TienIch_MayPhatDien")[0];
            string maybomnuoc = forms.GetValues("TienIch_MayBomNuoc")[0];
            string giengdong = forms.GetValues("TienIch_GiengDong")[0];
            var newItem = new TienIch
            {
                IDNhaDat = idnhadat,
                DongHoDien = bool.Parse(donghodien),
                DongHoNuoc = bool.Parse(donghonuoc),
                MayPhatDien = bool.Parse(mayphatdien),
                MayBomNuoc = bool.Parse(maybomnuoc),
                GiengDong = bool.Parse(giengdong)
            };
            var result = _repTienIch.Insert(newItem);
        }

        public void InsertTienIchKhac(FormCollection forms, int idnhadat)
        {
            string garaoto = forms.GetValues("TienIchKhac_GaraOto")[0];
            string maylanh = forms.GetValues("TienIchKhac_MayLanh")[0];
            string truyenhinhcap = forms.GetValues("TienIchKhac_TruyenHinhCap")[0];
            string sanvuon = forms.GetValues("TienIchKhac_SanVuon")[0];
            string maynuocnong = forms.GetValues("TienIchKhac_MayNuocNong")[0];
            string internet = forms.GetValues("TienIchKhac_Internet")[0];
            string dienthoai = forms.GetValues("TienIchKhac_DienThoai")[0];
            string chodauxehoi = forms.GetValues("TienIchKhac_ChoDauXeHoi")[0];
            string hoboi = forms.GetValues("TienIchKhac_HoBoi")[0];
            var newItem = new TienIchKhac
            {
                IDNhaDat = idnhadat,
                GaraOto = bool.Parse(garaoto),
                MayLanh = bool.Parse(maylanh),
                TruyenHinhCap = bool.Parse(truyenhinhcap),
                SanVuon = bool.Parse(sanvuon),
                MayNuocNong = bool.Parse(maynuocnong),
                Internet = bool.Parse(internet),
                DienThoai = bool.Parse(dienthoai),
                ChoDauXeHoi = bool.Parse(chodauxehoi),
                HoBoi = bool.Parse(hoboi)
            };
            var result = _repTienIchKhac.Insert(newItem);
        }

        public void InsertVanHoaXaHoi(FormCollection forms, int idnhadat)
        {
            string gantruongmaugiao = forms.GetValues("VanHoaXaHoi_GanTruongMauGiao")[0];
            string gantruongcap1 = forms.GetValues("VanHoaXaHoi_GanTruongCap1")[0];
            string gantruongcap2 = forms.GetValues("VanHoaXaHoi_GanTruongCap2")[0];
            string gantruongcap3 = forms.GetValues("VanHoaXaHoi_GanTruongCap3")[0];
            string gantruongdhcd = forms.GetValues("VanHoaXaHoi_GanTruongDHCD")[0];
            string gantrungtamthuongmai = forms.GetValues("VanHoaXaHoi_GanTrungTamThuongMai")[0];
            string khudantricao = forms.GetValues("VanHoaXaHoi_KhuDanTriCao")[0];
            string gancho = forms.GetValues("VanHoaXaHoi_GanCho")[0];
            string gancongvien = forms.GetValues("VanHoaXaHoi_GanCongVien")[0];
            string ganbenhvien = forms.GetValues("VanHoaXaHoi_GanBenhVien")[0];
            string gantrungtamgiaitri = forms.GetValues("VanHoaXaHoi_GanTrungTamGiaiTri")[0];
            string tienkinhdoanh = forms.GetValues("VanHoaXaHoi_TienKinhDoanh")[0];
            var newItem = new VanHoaXaHoi
            {
                IDNhaDat = idnhadat,
                GanTruongMauGiao = bool.Parse(gantruongmaugiao),
                GanTruongCap1 = bool.Parse(gantruongcap1),
                GanTruongCap2 = bool.Parse(gantruongcap2),
                GanTruongCap3 = bool.Parse(gantruongcap3),
                GanTruongDHCD = bool.Parse(gantruongdhcd),
                GanTrungTamThuongMai = bool.Parse(gantrungtamthuongmai),
                KhuDanTriCao = bool.Parse(khudantricao),
                GanCho = bool.Parse(gancho),
                GanCongVien = bool.Parse(gancongvien),
                GanBenhVien = bool.Parse(ganbenhvien),
                GanTrungTamGiaiTri = bool.Parse(gantrungtamgiaitri),
                TienKinhDoanh = bool.Parse(tienkinhdoanh)
            };
            var result = _repVanHoaXaHoi.Insert(newItem);
        }
    }
}
